package com.pzb.web.controller.order;

import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.core.page.TableDataInfo;
import com.pzb.framework.web.domain.server.Sys;
import com.pzb.order.domain.OdOrderDetail;
import com.pzb.order.domain.OdOrderSetting;
import com.pzb.order.service.IOdOrderDetailService;
import com.pzb.order.service.IOdOrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/refund")
public class RefundController extends BaseController {

    @Autowired
    private IOdOrderDetailService orderDetailService;
    @Autowired
    private IOdOrderSettingService orderSettingService;

    /**
     * 售后商品详情
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize,@RequestParam("state") Integer state,@RequestParam("extAppid") String extAppid)
    {
        String serviceStatus = null;
        if(state.intValue() == 0){
            serviceStatus = "2";
        }
        startPage();
       List<OdOrderDetail> detailList = orderDetailService.selectServiceDetailList(extAppid,getOpenid(),serviceStatus);
        return getDataTable(detailList);
    }

    /**
     * 售后商品详情
     */
    @GetMapping("/goods")
    public AjaxResult getGoodsDetail(@RequestParam("orderDetailId") Long orderDetailId,@RequestParam("extAppid")String extAppid)
    {
        OdOrderDetail orderDetail = orderDetailService.selectOdOrderDetailById(orderDetailId);
        //可进行售后状态 0可退换货  1可换货 2订单关闭
        int state = 0;
        OdOrderSetting orderSetting = orderSettingService.selectOdOrderSetting(extAppid);
        //已收货多少天
        long ms = new Date().getTime() - orderDetail.getFinishTime().getTime();
        //换货
        long returnMs = orderSetting.getReturnDays().longValue()*24*60*60*1000;

        if(ms > returnMs){
            state =1;
        }
        System.err.println("可退换货状态=====》"+state);
        orderDetail.setState(state);
        orderDetail.setOrderSetting(orderSetting);
        //最大退款金额
        BigDecimal serviceAmount = new BigDecimal(orderDetail.getAmount()).subtract(new BigDecimal(orderDetail.getCouponAmount())).subtract(new BigDecimal(orderDetail.getDiscountAmount())).subtract(new BigDecimal(orderDetail.getIntegralAmount()));
        orderDetail.setServiceAmount(String.valueOf(serviceAmount));
        //优惠后单价
        orderDetail.setUnitAmount(String.valueOf(serviceAmount.divide(new BigDecimal(orderDetail.getQuantity()),2,BigDecimal.ROUND_HALF_UP)));
        return AjaxResult.success(orderDetail);
    }


    /**
     * 提交售后
     */
    @PostMapping("/apply")
    public AjaxResult apply(@RequestBody OdOrderDetail orderDetail)
    {
        orderDetail.setApplicationTime(new Date());
        orderDetail.setServiceStatus("1");
        //售后金额
        orderDetail.setServiceAmount(String.valueOf(new BigDecimal(orderDetail.getUnitAmount()).multiply(new BigDecimal(orderDetail.getServiceQuantity()))));
        return AjaxResult.success(orderDetailService.updateOdOrderDetail(orderDetail));
    }

    /**
     * 售后商品详情
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("orderDetailId") Long orderDetailId,@RequestParam("extAppid")String extAppid)
    {
        OdOrderDetail orderDetail = orderDetailService.selectOdOrderDetailById(orderDetailId);
        OdOrderSetting orderSetting = orderSettingService.selectOdOrderSetting(extAppid);
        orderDetail.setOrderSetting(orderSetting);
        //最大退款金额
        BigDecimal serviceAmount = new BigDecimal(orderDetail.getAmount()).subtract(new BigDecimal(orderDetail.getCouponAmount())).subtract(new BigDecimal(orderDetail.getDiscountAmount())).subtract(new BigDecimal(orderDetail.getIntegralAmount()));
        orderDetail.setServiceAmount(String.valueOf(serviceAmount));
        //优惠后单价
        orderDetail.setUnitAmount(String.valueOf(serviceAmount.divide(new BigDecimal(orderDetail.getQuantity()),2,BigDecimal.ROUND_HALF_UP)));
        return AjaxResult.success(orderDetail);
    }

    /**
     * 售后发货
     */
    @PostMapping("/delivery")
    public AjaxResult delivery(@RequestBody OdOrderDetail orderDetail)
    {
        return AjaxResult.success(orderDetailService.updateOdOrderDetail(orderDetail));
    }
}
